package org.glut.backend.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.glut.backend.domain.Film;
import org.glut.backend.service.impl.FilmServiceImpl;
import org.glut.backend.util.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 *  前端控制器
 *
 * @author wubinbin
 * @since 2025-08-02 14:04
 */
@RestController
@Slf4j
public class FilmController {
    @Autowired
    FilmServiceImpl filmService;

    @Autowired
    RedisUtil redisUtil;

    @PostMapping("/public/film/query")
    public ResponseEntity<List<Object>> filmquery(@RequestBody Film condition) {
        log.info("影片查询条件{}",condition);
        QueryWrapper<Film> filmQueryWrapper = new QueryWrapper<>(Film.class);
        filmQueryWrapper.eq("filmid",condition.getFilmid());
        String key="film:query:"+condition.getFilmid();
        List filmList=null;

        if(!redisUtil.hasKey(key)){
            List _filmList=filmService.list(filmQueryWrapper);
            if(!CollectionUtils.isEmpty(_filmList)){
                //缓存
                redisUtil.lSet(key,_filmList);
                return ResponseEntity.ok(_filmList);
            }
        }else {
           filmList = redisUtil.lGet(key,0,-1);
        }
        return ResponseEntity.ok(filmList);
    }

    //查询全部
    @PostMapping("/public/film/queryall")
    public ResponseEntity<List<Object>> filmqueryall(@RequestBody Film film) {
        log.info("影片查询");
        QueryWrapper<Film> filmQueryWrapper = new QueryWrapper<>(Film.class);
        String key="film:queryall";
        List filmList=null;
        if(!redisUtil.hasKey(key)){
            List _filmList=filmService.list(filmQueryWrapper);
            if(!CollectionUtils.isEmpty(_filmList)){
                //缓存
                redisUtil.lSet(key,_filmList);
                return ResponseEntity.ok(_filmList);
            }
        }else {
            filmList = redisUtil.lGet(key,0,-1);
        }
        return ResponseEntity.ok(filmList);
    }
}
